package com.iflytek;

import android.annotation.SuppressLint;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.IOException;

/**
 * @ClassName MediaPlayUtils
 * @Description TODO
 * @Author zdl
 * @Date 2021/2/24 15:10
 * @Version 1.0
 */
public class MediaPlayUtils {

    static MediaPlayUtils mediaPlayUtils;

    public static MediaPlayUtils getInstance() {

        if (mediaPlayUtils == null) {
            mediaPlayUtils = new MediaPlayUtils();
        }
        return mediaPlayUtils;
    }


    MediaPlayer mediaPlayer;
    private final int PLAY_START = 1100;
    private final int PLAY_CONTINUE = 1101;

    PlayListener mplayListener;


    // 10  初始  11 播放  12  暂停  13 播放完毕
    private int palyState = 10;

    public void setPlayListener(PlayListener playListener) {
        mplayListener = playListener;
    }


    public interface PlayListener {

        void start();

        void pause();

        void completion();

        void timeupdate(int currtime, int totaltime);

        void prePared(int duration);
    }

    @SuppressLint("HandlerLeak")
    private final Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case PLAY_START:
                    myHandler.sendEmptyMessageDelayed(PLAY_CONTINUE, 200);
                    //播放时长
                    int duration = mediaPlayer.getDuration();
                    if (mplayListener != null)
                        mplayListener.prePared(duration);
                    break;
                case PLAY_CONTINUE:
                    myHandler.sendEmptyMessageDelayed(PLAY_CONTINUE, 200);
                    int currentPosition = mediaPlayer.getCurrentPosition();
                    int mduration = mediaPlayer.getDuration();
                    palyState = 11;
                    Log.e("tag", "currentPosition== " + currentPosition);
                    if (mplayListener != null)
                        mplayListener.timeupdate(currentPosition, mduration);
//                    if (seekBar != null) {
//                        seekBar.setProgress(currentPosition);
//                        tvNowtime.setText(format.format(new Date(currentPosition)));
//                    }
                    break;
            }

        }
    };

    public MediaPlayUtils() {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setLooping(false);

            mediaPlayer.setOnPreparedListener(mp -> {
                if (isplay) {
                    palyState = 10;
                    mediaPlayer.start();
                    myHandler.sendEmptyMessage(PLAY_START);
                }
                if (mplayListener != null) {
                    mplayListener.start();
                }

            });

            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    Log.e("tag", "onError: ");
                    return false;
                }
            });
            mediaPlayer.setOnCompletionListener(mp -> {
                if (mplayListener != null)
                    mplayListener.completion();
                palyState = 13;
                myHandler.removeCallbacksAndMessages(null);
            });
        } else {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
                mediaPlayer.release();
            }
        }
    }


    public MediaPlayer getMediaPlayer() {
        if (mediaPlayer == null) {
            Log.d("TAG", "mediaPlayer没有实例化 ");
        }
        return mediaPlayer;
    }


    private String filepath;

    public void setFilePath(String path) {
        filepath = path;
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
//        mediaPlayer.stop();
        try {
            palyState = 10;
            mediaPlayer.reset();
            mediaPlayer.setDataSource(path);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    private boolean isplay = true;

    public void setIsPreparePlay(boolean play) {
        isplay = play;
    }

    public void playandpause() {
        if (mediaPlayer != null) {
            if (palyState == 10) {
                mediaPlayer.prepareAsync();
            } else if (palyState == 13) {
                mediaPlayer.start();
                myHandler.sendEmptyMessageDelayed(PLAY_CONTINUE, 200);
                if (mplayListener != null)
                    mplayListener.start();
            } else {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                    if (mplayListener != null)
                        mplayListener.pause();
                    palyState = 12;
                } else {
                    mediaPlayer.start();
                    if (mplayListener != null)
                        mplayListener.start();
                }
            }
        }
    }


    public void stopPaly() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
            myHandler.removeCallbacksAndMessages(null);
        }
    }

    public void release() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.reset();
            myHandler.removeCallbacksAndMessages(null);
        }
    }
}
